package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Random;

@Component
public class SendSms {


    public static final String SecretId = "AKIDlKqBH6Y7cx4FydR8aorqeFbmhNoVqXid";
    public static final String SecretKey = "we9NK1ReeRVJp42kCPMhoQmvoGSO8pU5";
    /**
     * 阿里云发送验证短信码接口,
     *
     */
    public String  sendBatchSms(String phone){
        String YZCode = "";
        try{


            Random random = new Random();
            for (int i = 0; i < 6; i++) {
                YZCode += random.nextInt(10);
            }
            System.out.println(YZCode);

            Credential cred = new Credential(SecretId, SecretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            SmsClient client = new SmsClient(cred, "", clientProfile);

            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {"+86"+ phone};
            req.setPhoneNumberSet(phoneNumberSet1);

            req.setTemplateID("937905");
            req.setSign("纵生商城");

            String[] templateParamSet1 = {YZCode};
            req.setTemplateParamSet(templateParamSet1);

            req.setSmsSdkAppid("1400514227");

            SendSmsResponse resp = client.SendSms(req);

            System.out.println(SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return YZCode;
    }

    public static void main(String args[]){
        SendSms commonRpc = new SendSms();
        commonRpc.sendBatchSms("13949667279");
//        commonRpc.sendBatchSms("18337175150");
    }

}
